package com.share.lottery.activity;

import com.share.lottery.R;
import com.share.lottery.activity.loginandre.LoginActivity;
import com.share.lottery.activity.loginandre.RegGetDataActivity;
import com.share.lottery.activity.loginandre.RegisterActivity;
import com.share.lottery.utils.AppUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


/**
 * 启动界面 class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 * (2)是，则进入GuideActivity；否，则进入MainActivity (3)3s后执行(2)操作
 * 
 * @author zph
 * 
 */
public class SplashActivity extends Activity {
	boolean isFirstIn = false;
	private TextView tvLogin;
	private TextView tvRegiste;
	private static final int GO_HOME = 1000;
	private static final int GO_GUIDE = 1001;
	// 延迟3秒
	private static final long SPLASH_DELAY_MILLIS = 3000;

	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.splash);
		if (isLogin()) {
			goMain();
		}
		init();
		//		mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
		//		new WriteToSD(this);
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GO_HOME:
				goHome();
				break;
			case GO_GUIDE:
				goGuide();
				break;
			}
			super.handleMessage(msg);
		}
	};
	private boolean isLogin(){
		boolean isLogin = false;
		if (AppUtils.getUserBean() != null) {
			isLogin = true;
		}
		return isLogin;
	}
	private void goHome() {
		Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
		SplashActivity.this.startActivity(intent);
		SplashActivity.this.finish();
	}
	private void goGuide() {
		Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
		SplashActivity.this.startActivity(intent);
		SplashActivity.this.finish();
	}

	private void goMain() {
		Intent intent = new Intent(SplashActivity.this, JcMainActivtiy.class);
		SplashActivity.this.startActivity(intent);
		SplashActivity.this.finish();
	}
	private void init() {
		tvLogin = (TextView) findViewById(R.id.tv_dl);
		tvRegiste = (TextView) findViewById(R.id.tv_zc);

		tvRegiste.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {

				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), RegGetDataActivity.class);
				startActivity(intent);
			}
		});

		tvLogin.setOnClickListener(new OnClickListener() {


			public void onClick(View arg0) {
				goHome();

			}
		});

		// 读取SharedPreferences中需要的数据
		// 使用SharedPreferences来记录程序的使用次数
		SharedPreferences preferences = getSharedPreferences(
				SHAREDPREFERENCES_NAME, MODE_PRIVATE);

		// 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
		isFirstIn = preferences.getBoolean("isFirstIn", true);

		// 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
		if (!isFirstIn) {
			// 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
			//	mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
		} else {
			//	mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		StatService.onResume(SplashActivity.this);
		//		JPushInterface.onResume(SplashActivity.this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//		StatService.onPause(SplashActivity.this);
		//		JPushInterface.onPause(SplashActivity.this);
	}
}
